package com.app.repository.impl;

import com.app.model.main.Focus;
import com.app.repository.interfaces.FocusMapper;
import com.app.support.dao.MybatisDao;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author triplel
 * @create 2018-05-01 22:18
 */
@Repository
public class FocusMapperImpl implements FocusMapper {

    final String namespace = "com.app.dao.main.FocusMapper";

    @Autowired
    private MybatisDao mybatisDao;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return mybatisDao.delete(namespace,".deleteByPrimaryKey",id);
    }

    @Override
    public int insert(Focus record) {
        return mybatisDao.insert(namespace,".insert",record);
    }

    @Override
    public int insertSelective(Focus record) {
        return mybatisDao.insert(namespace,".insertSelective",record);
    }

    @Override
    public Focus selectByPrimaryKey(Long id) {
        return mybatisDao.get(namespace,".selectByPrimaryKey",id);
    }

    @Override
    public int updateByPrimaryKeySelective(Focus record) {
        return mybatisDao.update(namespace,".updateByPrimaryKeySelective",record);
    }

    @Override
    public int updateByPrimaryKey(Focus record) {
        return mybatisDao.update(namespace,".updateByPrimaryKey",record);
    }

    @Override
    public List<Long> getFansList(long userId) {
        return mybatisDao.getList(namespace,".getFansList",userId);
    }

    @Override
    public List<Long> getFocusList(long userId) {
        return mybatisDao.getList(namespace,".getFocusList",userId);
    }

    @Override
    public int getRelationBetween(Long fromId, Long toId) {
        Map<String,Object> map = new HashMap<>();
        map.put("fromId",fromId);
        map.put("toId",toId);
        return mybatisDao.get(namespace,".getRelationBetween",map);
    }

    @Override
    public long getFansNumber(long userId) {
        return mybatisDao.get(namespace,".getFansNumber",userId);
    }

    @Override
    public long getFocusNumber(long userId) {
        return mybatisDao.get(namespace,".getFocusNumber",userId);
    }

    @Override
    public List<Long> getFansListASC(long userId, long lastId, int number) {
        return getLimitedList(userId,lastId,number,".getFansListASC");
    }

    @Override
    public List<Long> getFansListDESC(long userId, long lastId, int number) {
        return getLimitedList(userId,lastId,number,".getFansListDESC");
    }

    @Override
    public List<Long> getFocusListASC(long userId, long lastId, int number) {
        return getLimitedList(userId,lastId,number,".getFocusListASC");
    }

    @Override
    public List<Long> getFocusListDESC(long userId, long lastId, int number) {
        return getLimitedList(userId,lastId,number,".getFocusListDESC");
    }

    @Override
    public Focus selectByParam(long from, long to) {
        Map<String,Object> map = new HashMap<>(2);
        map.put("from",from);
        map.put("to",to);
        return mybatisDao.get(namespace,".selectByParam",map);
    }

    private List<Long> getLimitedList(long userId,long lastId,int number,String key){
        Map<String,Object> map = new HashMap<>(2);
        map.put("userId",userId);
        map.put("lastId",lastId);
        return mybatisDao.getList(namespace,key,map,new RowBounds(0,number));
    }
}
